<?php

namespace App\Http\Controllers\Api\newWork;

use App\Http\Controllers\Api\BaseController;
use App\Models\Admin\Configs;
use Illuminate\Support\Str;
use Qcloud\Sms\SmsSingleSender;

class CommonController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function sendSms($mobile, $templateId, $data = []){
        $con = Configs::query()->where("mall_id", request()->mallData["id"])->first();
        // 短信应用SDK AppID
        $appid = $con->sms_appid; // 1400开头
        // 短信应用SDK AppKey
        $appkey = $con->sms_appkey;
        // 需要发送短信的手机号码
        // 签名
        $smsSign = $con->sms_sign;
        // 指定模板ID单发短信
        try {
            $ssender = new SmsSingleSender($appid, $appkey);
            $result = $ssender->sendWithParam("86", $mobile, $templateId,
                $data, $smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            $rsp = json_decode($result,true);
            if ($rsp['result'] == 0){
                return true;
            }else{
                return false;
            }
        } catch(\Exception $e) {
            return false;
        }
    }

    public function transferFields($data){
        if(empty($data)){
            return [];
        }
        if(is_array($data)){
            $newData = [];
            foreach ($data as $k => $v){
                if(!is_numeric($k)){
                    $k = Str::camel($k);
                }
                if(is_array($v)){
                    $v = $this->transferFields($v);
                }
                if(is_null($v)){
                    $v = '';
                }
                $newData[$k] = $v;
            }
            return $newData;
        }else{
            if(is_null($data)){
                return [];
            }else {
                return Str::camel($data);
            }
        }
    }
}
